package com.book.service;

import com.book.dutychain.AbstractBuinessHandler;
import com.book.dutychain.CityHandler;
import com.book.dutychain.builder.HandlerEnum;
import com.book.pojo.BusinessLaunch;
import com.book.pojo.UserInfo;
import com.book.repo.BusinessLaunchRepository;
import com.book.repo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description:TODO
 * @date 2023/10/23 21:57
 */
@Service
public class UserService {

    @Autowired
    private UserRepository repository;

    @Autowired
    private BusinessLaunchRepository businessLaunchRepository;

    @Value("${duty.chain}" )
    private String handlerType;

    // 用于记录当前的配置信息
    private String currentHandlerType;
    // 链表的当前节点
    private AbstractBuinessHandler currentHeadHandler;

    public String login(String account, String password) {
        UserInfo userInfo = repository.findByUserNameAndUserPassword(account, password);
        if(null == userInfo) {
            return "Login failed";
        }
        return "Login successfully";
    }

    public String register(UserInfo info) {
        if (checkUserExists(info.getUserName())) {
            throw new RuntimeException("User already registered");
        }
        info.setCreateDate(new Date());
        info.setEmail("12341515@163.com");
        repository.save(info);
        return "Register successfully";

    }


    public List<BusinessLaunch> launchBusiness(String targetCity, String targetSex, String targetProduct) {

        List<BusinessLaunch> allData = businessLaunchRepository.findAll();
        // 判断一下具体调用那个业务进行投放
        return handlerBuild().doHandler(allData, targetCity, targetSex, targetProduct);
    }

    public AbstractBuinessHandler handlerBuild() {
        // 获取配置信息
       if(handlerType == null){
           // 默认城市投放
           return new CityHandler();
       }
        // 第一次
        if(currentHandlerType == null){
            // 记录头结点
            currentHandlerType = handlerType;
        }
        if (currentHandlerType.equals(handlerType)&& currentHeadHandler != null){
            return currentHeadHandler;
        }else{
            System.out.println("说明配置文件有所变动");
            synchronized (this){
                // 创建真正链表节点
                AbstractBuinessHandler head = new CityHandler();
                // 创建中间链表节点
                // todo 这里会存在地址公有的问题 需要优化
                AbstractBuinessHandler preHead = head;

                String[] chiocesData = handlerType.split(",");
                for (int i = 0; i < chiocesData.length ; i++) {
                    try {
                        AbstractBuinessHandler handler = (AbstractBuinessHandler) Class.forName(HandlerEnum.valueOf(chiocesData[i]).getValue()).newInstance();
                        preHead.nextHandler = handler;
                        // 初始化头中间链表值，便于真正链表nexHandler操作
                        preHead = handler;
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
                // 重新赋值头节点
                this.currentHeadHandler = head.nextHandler;
                this.currentHandlerType = handlerType;
                return currentHeadHandler;
            }
        }



    }




    public boolean checkUserExists(String userName) {
        return repository.findByUserName(userName) == null ? false:true;
    }
}
